//
//  CarPortViewController.m
//  Qihuo
//
//  Created by Cheng Qi on 13-12-15.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//

#import "CarPortViewController.h"

@interface CarPortViewController ()
{
    NSArray *checkIDs;
}
@end

@implementation CarPortViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)dealloc
{
    [_contentView release];
    [_secondContentView release];
    [_thirdContentView release];
    [super dealloc];
}
#pragma mark--UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.backScrollV setContentOffset:CGPointMake(0, 0)];
    [self showUIWithArray:@[@"1",@"2"] andFloorId:[NSString stringWithFormat:@"%d",indexPath.row + 1]];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [super tableView:tableView numberOfRowsInSection:section];
    if (tableView==self.myTableView) {
        return [_dataArray count];
    }else
        return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [super tableView:tableView  cellForRowAtIndexPath:indexPath];
    if (tableView==self.myTableView)
    {
        self.mycell.textLabel.text =[_dataArray objectAtIndex:indexPath.row];
        
        NSIndexPath *path =[NSIndexPath indexPathForRow:0 inSection:0];
        [self.myTableView  selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionTop];
        return self.mycell;
    }else
        return nil;
}

#pragma mark--Request

#pragma mark---UIAction
-(void)jajajajaj:(UIButton *)btn
{
    UIImage *submitImage = [UIImage imageNamed:@"提交"];
    switch (btn.tag)
    {
        case 0:
        {
            if (_contentView.hidden==NO)
            {
                [_arr1 removeAllObjects];
                for (int i=1; i<7; i++){
                    UITextField *textField =(UITextField*)[_contentView viewWithTag:i];
                    NSMutableDictionary *dic =[[NSMutableDictionary alloc]initWithCapacity:0];
                    [dic setObject:[[NSString stringWithFormat:@"%@",textField.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"checkResult"];
                    NSLog(@"%@",textField.text);
                    [dic setObject:[NSString stringWithFormat:@"%d",i] forKey:@"checkId"];
                    UITextField *textF=(UITextField*)[_contentView viewWithTag:(10*i+i)];
                    [dic setObject:[[NSString stringWithFormat:@"%@",textF.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]forKey:@"process"];
                    [_arr1 addObject:dic];
                    [dic release];
                }
                [dic1 setObject:[NSString stringWithFormat:@"1"] forKey:@"floorId"];
                UITextView *view =(UITextView*)[_contentView viewWithTag:250];
                [dic1 setObject:[[NSString stringWithFormat:@"%@",view.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"floorRemark"];
                [dic1 setObject:_arr1 forKey:@"checkData"];
                if (_sendDataArray.count!=0) {
                    [_sendDataArray replaceObjectAtIndex:0 withObject:dic1];
                }else
                    [_sendDataArray addObject:dic1 ];
            }
            if (_secondContentView.hidden==NO) {
                [_arr2 removeAllObjects];
                for (int i=7; i<14; i++) {
                    UITextField *textField =(UITextField*)[_secondContentView viewWithTag:i];
                    NSMutableDictionary *dic =[[NSMutableDictionary alloc]initWithCapacity:0];
                    [dic setObject:[[NSString stringWithFormat:@"%@",textField.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"checkResult"];
                    [dic setObject:[[NSString stringWithFormat:@"%d",i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"checkId"];
                    UITextField*textF =(UITextField*)[_secondContentView viewWithTag:(10*i+i)];
                    [dic setObject:[NSString stringWithFormat:@"%@",[textF.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] forKey:@"process"];
                    [_arr2 addObject:dic];
                    [dic release];
                }
                [dic2 setObject:[NSString stringWithFormat:@"2"] forKey:@"floorId"];
                UITextView *view =(UITextView*)[_secondContentView  viewWithTag:250];
                [dic2 setObject:[[NSString stringWithFormat:@"%@",view.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]forKey:@"floorRemark"];
                [dic2 setObject:_arr2 forKey:@"checkData"];
                if (_sendDataArray.count>=2) {
                    [_sendDataArray replaceObjectAtIndex:1 withObject:dic2];
                }else
                    [_sendDataArray addObject:dic2];
            }
            if (_thirdContentView.hidden==NO) {
                [_arr3 removeAllObjects];
                for (int i=14; i<17; i++) {
                    UITextField *textField =(UITextField*)[_thirdContentView viewWithTag:i];
                    NSMutableDictionary *dic =[[NSMutableDictionary alloc]initWithCapacity:0];
                    [dic setObject:[[NSString stringWithFormat:@"%@",textField.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]forKey:@"checkResult"];
                    [dic setObject:[NSString stringWithFormat:@"%d",i] forKey:@"checkId"];
                    UITextField*textG =(UITextField*)[_thirdContentView viewWithTag:(10*i+i)];
                    [dic setObject:[[NSString stringWithFormat:@"%@",textG.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"process"];
                    [_arr3 addObject:dic];
                    [dic release];
                }
                [dic3 setObject:[NSString stringWithFormat:@"3"] forKey:@"floorId"];
                UITextView *view =(UITextView*)[_thirdContentView  viewWithTag:250];
                [dic3 setObject:[[NSString stringWithFormat:@"%@",view.text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"floorRemark"];
                [dic3 setObject:_arr3 forKey:@"checkData"];
                if (_sendDataArray.count>=3) {
                    [_sendDataArray replaceObjectAtIndex:2 withObject:dic3];
                }else
                    [_sendDataArray addObject:dic3];
            }
            if (btn.currentImage ==submitImage)
            {
                NSLog(@"sendDataArray=%@",_sendDataArray);
                [self.alertV textFieldAtIndex:0].text =[[Appinfomation shareIstance].loginDic valueForKey:@"user"];
                [self.alertV show];
                self.sendUrl = addCar;
            }else{
                UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"数据保存成功。。。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [myAlert show];
                self.sendFloorDatas = _sendDataArray;
                [myAlert release];
            }
            
            [self gotoNextFloor];
            NSIndexPath *path0 =[NSIndexPath indexPathForRow:0 inSection:0];
            NSIndexPath *path1 =[NSIndexPath indexPathForRow:1 inSection:0];
            NSIndexPath *path2 =[NSIndexPath indexPathForRow:2 inSection:0];
            XunJianCell *cell1 =(XunJianCell*)[self.myTableView cellForRowAtIndexPath:path0];
            XunJianCell *cell2 =(XunJianCell*)[self.myTableView cellForRowAtIndexPath:path1];
            XunJianCell *cell3 =(XunJianCell*)[self.myTableView cellForRowAtIndexPath:path2];
            if (cell1.gouView.hidden==NO&&cell2.gouView.hidden==NO&&cell3.gouView.hidden==NO)
            {
                [btn setImage:submitImage forState:UIControlStateNormal];
            }
        }
            break;
        case 1:
        {
            self.navigationController.navigationBar.hidden = YES;
            self.blackGroundV.hidden = NO;
        }
            break;
        case 2:
        {
            self.navigationController.navigationBar.hidden = YES;
            self.blackStopV.hidden = NO;
        }
            break;
            
        default:
            break;
    }

}
#pragma mark---Request

#pragma mark-Resource
-(void)addDataArray
{
    _sendDataArray =[[NSMutableArray alloc]initWithCapacity:0];
    _arr1 =[[NSMutableArray alloc]initWithCapacity:0];
    _arr2 =[[NSMutableArray alloc]initWithCapacity:0];
    _arr3 =[[NSMutableArray alloc]initWithCapacity:0];
    dic1 =[[NSMutableDictionary alloc]initWithCapacity:0];
    dic2 =[[NSMutableDictionary alloc]initWithCapacity:0];
    dic3 =[[NSMutableDictionary alloc]initWithCapacity:0];
    _dataArray = @[@"车辆情况",@"设备情况",@"清洁情况"];
    self.sendCheckDatas = [NSMutableArray array];
    for (int i = 0; i < checkIDs.count; i ++)
    {
        [self.sendCheckDatas addObject:@{@"checkId": [checkIDs objectAtIndex:i],@"result":@"",@"process":@""}];
    }
    self.commentTv = [[UITextView alloc]init];
}

-(void)changeTableView
{
    UIImage *blackImage = [UIImage imageNamed:@"黑底"];
    self.myTableView.frame=CGRectMake(interval111/2, 64, self.leftTableImage.size.width, screenHeight - blackImage.size.height - 64);
    self.whiteImageView.frame=CGRectMake(self.myTableView.frame.origin.x + self.myTableView.frame.size.width, self.myTableView.frame.origin.y, scrennWidth - (self.myTableView.frame.origin.x)*2 -self.myTableView.frame.size.width, self.myTableView.frame.size.height);
    self.backScrollV.frame =CGRectMake(10, 10, self.whiteImageView.frame.size.width - 20, self.whiteImageView.frame.size.height - 20);
    //view
    _contentView =[[[NSBundle mainBundle]loadNibNamed:@"Car" owner:self options:nil]lastObject];
    _secondContentView =[[[NSBundle mainBundle]loadNibNamed:@"CarSecond" owner:self options:nil]lastObject];
    _thirdContentView =[[[NSBundle mainBundle]loadNibNamed:@"CarThird" owner:self options:nil]lastObject];
    _viewArray =[[NSArray alloc]initWithObjects:_contentView,_secondContentView,_thirdContentView,nil];
    [self.backScrollV addSubview:_contentView];
    [self.backScrollV addSubview:_secondContentView];
    [self.backScrollV addSubview:_thirdContentView];
    _contentView.hidden = YES;
    _secondContentView.hidden=YES;
    _thirdContentView.hidden=YES;
    [self.backScrollV setContentSize:CGSizeMake(561, 1200)];
    
    UIImageView *imageView =(UIImageView*)[_contentView viewWithTag:12345];
    imageView.userInteractionEnabled=YES;
}
- (void)showUIWithArray:(NSArray *)array andFloorId:(NSString *)floorId
{
    self.floorIdQuanju = floorId;
    self.sendCheckDatas = [NSMutableArray array];
    switch ([self.floorIdQuanju integerValue]) {
        case 1:
        {
            checkIDs = @[@"1",@"2",@"3",@"4",@"5",@"6"];
        }
            break;
        case 2:
        {
            checkIDs = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7"];
        }
            break;
        case 3:
        {
            checkIDs = @[@"1",@"2",@"3"];
        }
            break;
            
        default:
            break;
    }
    for (int i = 0; i < checkIDs.count; i ++)
    {
        [self.sendCheckDatas addObject:@{@"checkId": [checkIDs objectAtIndex:i],@"result":@"",@"process":@""}];
    }

    switch ([floorId integerValue]-1) {
        case 0:
        {
            _secondContentView.hidden=YES;
            _thirdContentView.hidden=YES;
            _contentView.hidden=NO;
            [self.backScrollV setContentSize:CGSizeMake(561, 1200)];
        }
            break;
        case 1:
        {
            _contentView.hidden=YES;
            _thirdContentView.hidden=YES;
            _secondContentView.hidden=NO;
            [self.backScrollV setContentSize:CGSizeMake(561, 1300)];
        }
            break;
        case 2:
        {
            _thirdContentView.hidden=NO;
            _contentView.hidden=YES;
            _secondContentView.hidden=YES;
            [self.backScrollV setContentSize:CGSizeMake(561,700)];
        }
            break;
        default:
            break;
    }

}
#pragma mark---UITextFieldDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    UITextView *viee1 =(UITextView*)[_contentView viewWithTag:54321];
    UITextView *viee2 =(UITextView*)[_secondContentView viewWithTag:54321];
    UITextView *viee3 =(UITextView*)[_thirdContentView viewWithTag:54321];
    if (viee1 == textView)
    {
        self.commentTv.text = textView.text;
    }
    if (viee2 == textView)
    {
        self.commentTv.text = textView.text;
    }
    if (viee3 == textView )
    {
        self.commentTv.text = textView.text;
    }
}
//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//
//    
//    if (alertView==self.alertV)
//    {
//        switch (buttonIndex)
//        {
//            case 1:
//            {
//                [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
//                if ([alertView textFieldAtIndex:0].text.length == 0) {
//                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！" message:@"请输入用户名！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//                    [alert show];
//                }else if ([alertView textFieldAtIndex:1].text.length == 0){
//                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！" message:@"请输入密码！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//                    [alert show];
//                }else{
//                    NSURL *loginUrl = [NSURL URLWithString:loginIP];
//                   ASIFormDataRequest *loginRequest = [[ASIFormDataRequest alloc] initWithURL:loginUrl];
//                    [loginRequest setPostValue:[NSString stringWithFormat:@"%@",[alertView textFieldAtIndex:0].text] forKey:@"user"];
//                    [loginRequest setPostValue:[NSString stringWithFormat:@"%@",[alertView textFieldAtIndex:1].text] forKey:@"password"];
//                    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//                    [loginRequest startSynchronous];
//                    NSString *response = [loginRequest responseString];
//                    NSDictionary *responseDic = [response JSONValue];
//                    NSDictionary *DataDic = [responseDic objectForKey:@"data"];
//                    [MBProgressHUD hideHUDForView:self.view animated:YES];
//                    if (DataDic == nil) {
//                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！" message:@"网络无法连接！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//                        [alert show];
//                    }else if ([DataDic isEqual:[NSString stringWithFormat:@"NONE"]]) {
//                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！" message:@"用户名不存在！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//                        [alert show];
//                    }else if ([DataDic isEqual:[NSString stringWithFormat:@"FAILURE"]]){
//                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！" message:@"用户密码错误！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//                        [alert show];
//                    }
//                    else
//                    {
//                        [self loginSendData];
//                    }
//                }
//            }
//                break;
//                
//            default:
//                
//                break;
//        }
//    }
//    
//}
#pragma mark - keyboardNotification
CGSize keyboardSize;
- (void) keyboardWasShown:(NSNotification *) notif
{
//    NSDictionary *info = [notif userInfo];
//    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
//    keyboardSize = [value CGRectValue].size;
//    NSLog(@"keyBoard:%f", keyboardSize.width);  //216
//    _backScrollV.contentSize = CGSizeMake(_backScrollV.frame.size.width, _commentTv.frame.origin.y + _commentTv.frame.size.height + interval111+406);
//    if (self.blackGroundV.hidden == NO)
//    {
//        [UIView animateWithDuration:.5 animations:^{
//            self.blackGroundV.frame = CGRectMake(0, -150, 1024, 768);
//        }];
//    }
//    if (self.blackStopV.hidden == NO) {
//        [UIView animateWithDuration:.5 animations:^{
//            self.blackStopV.frame = CGRectMake(0, -150, 1024, 768);
//        }];
//    }
}
- (void) keyboardWasHidden:(NSNotification *) notif
{
//    NSDictionary *info = [notif userInfo];
//    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
//    CGSize keyboardSize = [value CGRectValue].size;
//    NSLog(@"keyboardWasHidden keyBoard:%f", keyboardSize.width);
//    self.backScrollV.contentSize = CGSizeMake(self.backScrollV.frame.size.width, _commentTv.frame.origin.y + _commentTv.frame.size.height + interval111);
//    if (self.blackGroundV.hidden == NO)
//    {
//        [UIView animateWithDuration:0.5 animations:^{
//            self.blackGroundV.frame = CGRectMake(0, 0, 1024, 768);
//        }];
//    }
//    if (self.blackStopV.hidden == NO) {
//        [UIView animateWithDuration:.5 animations:^{
//            self.blackStopV.frame = CGRectMake(0, 0, 1024, 768);
//        }];
//    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title =@"车库管理检查表";
    self.checkXunJianArea = @"3";
//    [self sendRequest];
    [self addDataArray];
    [self changeTableView];
    for (int i = 0; i < 3;i++)
    {
        [self.floorIds addObject:[NSString stringWithFormat:@"%d",i+1]];
        [self.sendFloorDatas addObject:@""];
        [self.xunJianCellInfos addObject:@{@"hidden":@"1",@"floorId":[NSString stringWithFormat:@"%d",i+1]}];
    }
    
    [self showUIWithArray:@[@""] andFloorId:@"1"];
    self.sendUrl = [NSString stringWithFormat:@"%@",addCar];
//    NSIndexPath *indexs = [NSIndexPath indexPathForRow:0 inSection:0];
//    [self.myTableView selectRowAtIndexPath:indexs animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
